package com.JavaTest7;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class BookManagement {
    ArrayList<Book> books;

    public BookManagement(){
        books = new ArrayList<Book>();
    }

    public BookManagement(ArrayList<Book> b){
        super();
        this.books = b;
    }

    public void addBook(Book b){
        this.books.add(b);
    }

    public void deleteBook(int id){
        for(Book i : books){
            if(id == i.getB_ID()){
                this.books.remove(i);
                return;
            }
        }
        System.out.println("无该编号书籍！");
    }

    public String searchBookByID(int id){
        for(Book i : books){
            if(id == i.getB_ID()){
                return i.toString();
            }
        }
        return "NULL!";
    }

    public String searchBookByName(String n){
        for(Book i : books){
            if(n.equals(i.getB_Name()))
                return i.toString();
        }
        return "NULL!";
    }

    public void sortByID(){
        books.sort(new Comparator<Book>() {
            @Override
            public int compare(Book b1, Book b2) {
                return b2.getB_ID() - b1.getB_ID();
            }
        });
    }

    public void printBookInfo(){
        for(Book i : books){
            System.out.println(i.toString());
        }
    }
}
